var myw = screen.availWidth * 0.5;
var myh = screen.availHeight* 0.7;
$(function () {
    var allData = {};
    //begin列表信息
    $("#role_datagrid").datagrid({
        url: "/role/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100, hidden: true},
            {field: "sn", title: "描述", width: 100},
            {field: "name", title: "名称", width: 100}
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber: 1,
        pageSize: 16,
        pageList: [16, 25, 35, 50],
        toolbar: "#role_tb"
    });
    //end列表信息

    //begin添加/编辑对话框
    $("#role_dialog").dialog({
        width: myw,
        height: myh,
        maximizable:true,
        minimizable:true,
        collapsible:true,
        buttons: "#role_bb",
        closed: true
    });
    //end添加/编辑对话框

    //begin所有权限
    $("#allPermission").datagrid({
        title: "所有权限",
        width: 250,
        height: 350,
        url: "permission/queryPermissionList",
        columns: [[
            {field: "id", title: "编号", hidden: true},
            {field: "name", title: '权限名称', align: 'center', width: 150}
        ]],
        fitColumns: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        onClickRow: function (index, row) {
            //console.log(index, row);
            $("#allPermission").datagrid("deleteRow", index);
            $("#selfPermission").datagrid("appendRow", row);
        },
        //在数据加载成功的时候触发
        onLoadSuccess:function (data) {
            //{total: 9, rows: Array(3)}
            //console.log(data);

            //加载缓存数据(合并data对象的数据,返回permission/list数据)
            allData = $.extend(true,{},data);

        }

    });
    //end所有权限

    //begin自身权限
    $("#selfPermission").datagrid({
        title: "自身权限",
        width: 250,
        height: 350,
        columns: [[
            {field: "id", title: "编号", hidden: true},
            {field: 'name', title: '权限名称', align: 'center', width: 150}
        ]],
        fitColumns: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        onClickRow: function (index, row) {
            //console.log(index,row);
            $("#selfPermission").datagrid("deleteRow", index);
            $("#allPermission").datagrid("appendRow", row);
        },
        //数据加载成功后
        onLoadSuccess:function(data){
            var selfPermissionRows = $("#selfPermission").datagrid("getRows");

            var ids = $.map(selfPermissionRows,function (selfPermissionRow) {
                console.log(selfPermissionRow);
                return selfPermissionRow.id;
            })
                console.log(ids)
            var allPremissionRows = $("#allPermission").datagrid("getRows");

            for(var i = allPremissionRows.length - 1 ; i >= 0 ; i --){
                if($.inArray(allPremissionRows[i].id,ids) >= 0){
                    $("#allPermission").datagrid("deleteRow",i);
                }
            }
        }
    });
    //end自身权限

    //begin处理功能方法的对象
    var obj = {
        //begin添加按钮
        add: function () {
            $("#role_form").form("clear");
            $("#role_dialog").dialog("setTitle", "角色添加");
            $("#role_dialog").dialog("open");
            $("#selfPermission").datagrid("loadData", {"total": 0, "rows": []});
            $("#allPermission").datagrid("loadData", allData);
        },
        //end添加按钮

        //begin编辑按钮
        edit: function () {
            $("#role_form").form("clear");
            $("#role_dialog").dialog("setTitle", "角色编辑");
            var row = $("#role_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            var roleId = row.id;
            $.get("/systemMenu/querySystemMenuByRoleId",{roleId:roleId},function (data) {
                console.log(data)
                var arrays = $.map(data,function(n){
                    console.log(n)
                    return n.id;
                });
                $("#systemMenu").combobox("setValues",arrays);
            });

            $("#role_form").form("load", row);
            $("#role_dialog").dialog("open");

            //在弹框前,加载缓存的数据
            $("#allPermission").datagrid("loadData",allData);
            $("#selfPermission").datagrid("loadData",{"total":0,"rows":{}})
            //发送请求数据查找自身的权限
            var options = $("#selfPermission").datagrid("options");
            //{id: "selfPermission", title: "自身权限", iconCls: null, width: 250, height: 350, …}
            //console.log(options);
            options.url = "/permission/queryPermissionByRoleId?roleId="+row.id;
            $("#selfPermission").datagrid("load");
        },
        //end编辑按钮

        //begin取消按钮
        cancel: function () {
            $("#role_dialog").dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        save: function () {
            var roleId = $("#roleId").val();
            var url;
            if (roleId) {
                url = "/role/update"
            } else {
                url = "/role/save"
            }
            $("#role_form").form("submit", {
                url: url,
                //begin提交之前的回调函数
                onSubmit: function(param){
                    //{}:  permission[0].id
                    //console.log(param);

                    //拿到自身权限的所有行
                    var rows = $("#selfPermission").datagrid("getRows");
                    for (var i = 0; i < rows.length; i++) {
                        param["permissions["+i+"].id"] = rows[i].id;
                    }
                    var menuId = $("#systemMenu").combobox("getValues");
                    for(var i = 0; i < menuId.length; i++){
                        param["systemMenus["+i+"].id"] = menuId[i];
                    }
                },
                //end提交之前的回调函数
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#role_dialog").dialog("close");
                        $("#role_datagrid").datagrid("reload");
                    }
                }
            });
        },
        //end保存按钮

        //begin删除按钮
        remove: function () {
            var row = $("#role_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('确认', '您确认要操作此行吗？', function (r) {
                if (r) {
                    //注意这里的id要从当前选中的行中获取
                    var roleId = row.id;
                    $.get("/role/delete", {roleId: roleId}, function (data) {
                        if (!data.success) {
                            $.messager.alert('温馨提示', data.msg);
                        } else {
                            $.messager.alert('温馨提示', '操作成功!');
                            $("#role_datagrid").datagrid("reload");
                        }
                    });
                }
            });

        },
        //end删除按钮

        //begin刷新按钮
        reload: function () {
            $("#role_datagrid").datagrid("reload")
        }
        //end刷新按钮
    };
    //end处理功能方法的对象

    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法

});